package com.powernode.teatraceability.service.contract.impl;

import com.powernode.teatraceability.constant.CustomerConstant;
import com.powernode.teatraceability.raw.Enterprise;
import com.powernode.teatraceability.service.contract.EnterpriseCertificateService;
import com.powernode.teatraceability.vo.EnterpriseCertificateVo;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.abi.datatypes.generated.tuples.generated.Tuple7;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;

/**
 * ClassName: EnterpriseImpl
 * Description:
 *
 * @Author: jdz
 * @Create: 2023/5/3 - 12:02
 */
@Service
@Slf4j
public class EnterpriseCertificateImpl implements EnterpriseCertificateService {
    @Value("${system.contract.enterpriseAddress}")
    private String contractAddress;

    @Resource
    private Client client;
    @Override
    public boolean applyEnterpriseCertificate(String _address, String _name, String _fzr, String _certificateStandard, String _certificateMaterial) {
        final Enterprise enterprise = Enterprise.load(contractAddress, client, client.getCryptoSuite().createKeyPair());
        final TransactionReceipt transactionReceipt = enterprise.applyEnterpriseCertificate(_address, _name, _fzr, _certificateStandard, _certificateMaterial);
        log.info("applyEnterpriseCertificate traction receipt {}", transactionReceipt.toString());
        final String status = transactionReceipt.getStatus();
        return CustomerConstant.SUCCESS_STATUS.equals(status);
    }

    @Override
    public String approveEnterpriseCertificate(String _address, BigInteger _index) {
        final Enterprise enterprise = Enterprise.load(contractAddress, client, client.getCryptoSuite().createKeyPair());
        final TransactionReceipt transactionReceipt = enterprise.approveEnterpriseCertificate(_address, _index);
        log.info("approveEnterpriseCertificate traction receipt {}", transactionReceipt.toString());
        final String status = transactionReceipt.getStatus();
        if (CustomerConstant.SUCCESS_STATUS.equals(status)) {
            return transactionReceipt.getTransactionHash();
        }
        return null;
    }

    @Override
    public boolean changeEnterpriseCertificate(String _address, BigInteger _index, String _name, String _fzr, String _certificateStandard, String _certificateMaterial) {
        final Enterprise enterprise = Enterprise.load(contractAddress, client, client.getCryptoSuite().createKeyPair());
        final TransactionReceipt transactionReceipt = enterprise.changeEnterpriseCertificate(_address, _index, _name, _fzr, _certificateStandard, _certificateMaterial);
        log.info("changeEnterpriseCertificate traction receipt {}", transactionReceipt.toString());
        final String status = transactionReceipt.getStatus();
        return CustomerConstant.SUCCESS_STATUS.equals(status);
    }

    @Override
    public EnterpriseCertificateVo getEnterpriseCertificate(String _address, BigInteger _index) {
        final Enterprise enterprise = Enterprise.load(contractAddress, client, client.getCryptoSuite().createKeyPair());
        try {
            final Tuple7<String, String, String, String, String, BigInteger, Boolean> enterpriseCertificate = enterprise.getEnterpriseCertificate(_address, _index);
            if (enterpriseCertificate.getValue1().startsWith("0x00000000000000")) {
                return null;
            } else {
                return new EnterpriseCertificateVo(
                        enterpriseCertificate.getValue1(),
                        enterpriseCertificate.getValue2(),
                        enterpriseCertificate.getValue3(),
                        enterpriseCertificate.getValue4(),
                        enterpriseCertificate.getValue5(),
                        enterpriseCertificate.getValue6(),
                        enterpriseCertificate.getValue7());
            }
        } catch (ContractException e) {
            e.printStackTrace();
            return null;
        }
    }
}
